// Rectangle.cpp: implementation of the CRectangle class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DrawGraph.h"
#include "Rectangle.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRectangle::CRectangle(CPoint StartPoint,CPoint EndPoint)
{
	this->m_pStartPoint = StartPoint;
	this->m_pEndPoint = EndPoint;
	if(m_rgn.m_hObject)
	{
		m_rgn.DeleteObject();
	}
	m_rgn.CreateRectRgn(StartPoint.x,StartPoint.y,EndPoint.x,EndPoint.y);

	SetTheShapeGravity(StartPoint,EndPoint);

	int x,y;
	x = (int)fabs(this->m_pStartPoint.x-this->m_pEndPoint.x);
	y = (int)fabs(this->m_pStartPoint.y-this->m_pEndPoint.y);
	this->circum = (x+y)*2;
	this->area = x*y;

	m_SumCircum +=this->circum;
	m_SumArea +=this->area;

}

CRectangle::~CRectangle()
{
	m_SumArea  -= this->area;
	m_SumCircum -=this->circum;
}
CRectangle::CRectangle()
{
	
}
void CRectangle::Zoom(BOOL zoom,UINT x)
{
	CPoint tmp;
	double t = x/(1.0*(x+1));
   	if(zoom)
	{	
		tmp = CPoint((this->m_pStartPoint.x-this->m_CenterOfGravity.x)*x,
			(this->m_pStartPoint.y-this->m_CenterOfGravity.y)*x);
		this->m_pStartPoint = this->m_pStartPoint +tmp; ;
		this->m_pEndPoint = this->m_pEndPoint - tmp;
	}
	else
	{
		tmp = CPoint((int)((this->m_pStartPoint.x-this->m_CenterOfGravity.x)*t),
			(int)((this->m_pStartPoint.y-this->m_CenterOfGravity.y)*t));
		this->m_pStartPoint = this->m_pStartPoint - tmp;
		this->m_pEndPoint = this->m_pEndPoint + tmp;

	}
	if( m_rgn.m_hObject )
		m_rgn.DeleteObject();
	m_rgn.CreateRectRgn(this->m_pStartPoint.x,this->m_pStartPoint.y,
			this->m_pEndPoint.x,this->m_pEndPoint.y);
	ReSetArea( );
	ReSetCircum( );
}
void CRectangle::SetTheShapeInfor(COLORREF clr,
		UINT width,BOOL IsSolid )
{
	this->m_clr = clr;
	this->m_nLineWidth = width;
	this->m_BrushIsSolid = IsSolid;
}
void CRectangle::ResetTheShape(const CPoint &p1,const CPoint &p2)
{
	this->m_pStartPoint = p1;
	this->m_pEndPoint = p2;
	if(m_rgn.m_hObject)
		m_rgn.DeleteObject();
	m_rgn.CreateRectRgn(p1.x,p1.y,p2.x,p2.y);
	SetTheShapeGravity(p1,p2);
}

void CRectangle::MoveReset(const CPoint &p)
{
	ResetTheShape(this->m_pStartPoint-p,
					this->m_pEndPoint -p);
	
}

void CRectangle::SetTheShapeGravity(const CPoint StartPoint, const CPoint EndPoint)
{
	int x, y;
	x = (this->m_pStartPoint.x+this->m_pEndPoint.x)/2;
	y = (this->m_pStartPoint.y+this->m_pEndPoint.y)/2;
	this->m_CenterOfGravity =CPoint(x,y);
}

void CRectangle::ReSetArea( )
{
	m_SumArea -=this->area;
	this->area = fabs((this->m_pStartPoint.x-this->m_pEndPoint.x)*(this->m_pStartPoint.y-this->m_pEndPoint.y));	
	m_SumArea +=this->area;
}

void CRectangle::ReSetCircum( )
{
	m_SumCircum -=this->circum;
	this->circum = (fabs(this->m_pStartPoint.x-this->m_pEndPoint.x)+fabs(this->m_pStartPoint.y-this->m_pEndPoint.y))*2;
	m_SumCircum +=this->circum;
}
